<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015-5-30
 * Time: 14:57
 */

namespace zhtfrontend\controllers;

use zhtfrontend\components\services\goods\CreateAction;
use zhtfrontend\components\services\goods\DeleteAction;
use zhtfrontend\components\services\goods\DetailsAction;
use zhtfrontend\components\services\goods\GoodsIndexAction;
use zhtfrontend\components\services\goods\SetDistributionAction;
use zhtfrontend\components\services\goods\UpdateAction;
use common\models\datas\Admin;
use zhtfrontend\components\bases\BaseController;
use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use zhtfrontend\models\datas\AccountRecord;
use yii\web\Response;
use common\models\datas\Customer;
use yii\helpers\Json;
use yii\data\Pagination;

class GoodsproductController extends BaseController
{

//    public $layout = '@app/views/layouts/iframe.php';

    public function actions()
    {
        return [
            'index' => [
                'class' => GoodsIndexAction::className(),
            ],
            'create' => [
                'class' => CreateAction::className(),
            ],
            'update' => [
                'class' => UpdateAction::className(),
            ],
            'details' => [
                'class' => DetailsAction::className(),
            ],
            'delete' => [
                'class' => DeleteAction::className(),
            ],
            'set' => [
                'class' => SetDistributionAction::className(),
            ]
        ];
    }
//    public function actionIndex()
//    {
//        if (\Yii::$app->request->isAjax) {
//            //var_dump(\Yii::$app->request->post('param'));die;
//            if (\Yii::$app->request->post('param') != null) $param = Json::decode(\Yii::$app->request->post('param'));
//            \Yii::$app->response->format = Response::FORMAT_JSON;
//            $pageSize = !empty($param['pageSize']) ? $param['pageSize'] : 12;
//            $accountrecord = AccountRecord::find()->orderBy('id ASC');
//            if (!empty($param['searchData'])) {
//                $search_data = array_filter($param['searchData']);
//
//                if (!empty($search_data['customer_name'])) {
//                    $customer = Customer::find()->select('id')->Where(['like', 'name', $search_data['customer_name']])->column();
//                    $accountrecord->andWhere(['in', 'customer_id', $customer]);
//                }
//                if (!empty($search_data['admin_name'])) {
//                    $user = Admin::find()->select('id')->Where(['like', 'username', $search_data['admin_name']])->column();
//                    $accountrecord->andWhere(['in', 'user_id', $user]);
//                }
//                if (!empty($search_data['created_at_begin']))
//                    $accountrecord->andWhere(['>=', 'created_at', strtotime($search_data['created_at_begin'])]);
//                if (!empty($search_data['created_at_end']))
//                    $accountrecord->andWhere(['<=', 'created_at', strtotime($search_data['created_at_end'])]);
//
//            }
//
//            $pages = new Pagination(['totalCount' => $accountrecord->count(), 'pageSize' => $pageSize]);
//            isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
//            $model = $accountrecord->offset($pages->offset)->limit($pages->limit)->all();
//            $ajax_object = new AjaxObject();
//            $ajax_object->data = Fun::asArray($model, ['customer_name', 'admin_name']);
//            $ajax_object->pages = $pages;
//            return $ajax_object;
//        }
//        return $this->render('index');
//    }

    /**
     *  充值统计
     */
    public function actionRecharge()
    {
        $param['searchData'] = \Yii::$app->request->get();
        $pageSize = !empty($param['pageSize']) ? $param['pageSize'] : 12;
        $accountrecord = \common\models\datas\AccountRecord::find()->where(['state' => 1])->orderBy('id DESC');
        if (!empty($param['searchData'])) {
            $search_data = array_filter($param['searchData']);
            if (!empty($search_data['name'])) {
                $customer = Customer::find()->select('id')->Where(['like', 'name', $search_data['name']])->column();
                if ($customer != null)
                    $accountrecord->andWhere(['in', 'customer_id', $customer]);
            }
            if (!empty($search_data['mobile'])) {
                $customer = Customer::find()->Where(['mobile' => $search_data['mobile']])->one();
                if ($customer != null)
                    $accountrecord->andWhere(['customer_id' => $customer->id]);
            }
            if (isset($search_data['searchWay']) && $search_data['searchWay'] != null) {
                if ($search_data['searchWay'] == 2) { //按日搜索
                    $accountrecord->andWhere(['between', 'created_at', strtotime(date('Y-m-d', strtotime($search_data['searchDate']))), strtotime(date('Y-m-d', strtotime("+1 day", strtotime($search_data['searchDate']))))]);
                } else { //按月搜索
                    $accountrecord->andWhere(['between', 'created_at', strtotime(date('Y-m', strtotime($search_data['searchDate']))), strtotime(date('Y-m', strtotime("next month", strtotime($search_data['searchDate']))))]);
                }
            }

        }
        $totals = $accountrecord->sum('cash');
        $pages = new Pagination(['totalCount' => $accountrecord->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $model = $accountrecord->offset($pages->offset)->limit($pages->limit)->all();
        $array = Fun::asArray($model, ['customer_name', 'admin_name']);
        $content = "<span style='margin-left: 20px'>总计: {$accountrecord->count()} 条</span><span style='margin-left: 20px'>金额总计：{$totals} 元</span>";

        return $this->render('recharge',['data'=>$array,'pages'=>$pages,'content'=>$content]);
    }
}